package pl.edu.pk.rz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pl.edu.pk.rz.entity.MenuEntry;
import pl.edu.pk.rz.entity.Page;

@Controller
public class TemplateController extends AbstractController {

	@RequestMapping(value = "show-page", method = RequestMethod.GET)
	public ModelAndView showPage(@RequestParam(required = false, defaultValue = "1", value = "page-id") int pageId) {
		logger.info("Show page id=" + pageId);
		Page page = pageService.read(pageId);
		logger.info("Found page " + page.getTitle() + " by id=" + pageId);

		List<Page> pages = pageService.listAll();
		
		List<MenuEntry> menuEntries = menuBoxService.listAll();
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("page", page);
		model.put("pages", pages);
		model.put("menuEntries", menuEntries);
		return new ModelAndView(VIEW_TEMPLATE, MODEL, model);
	}
}
